package com.xuecheng.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Category;
import com.xuecheng.basic.mapper.CategoryMapper;
import com.xuecheng.basic.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    // 课程分类
    @Override
    public ResponseResult kechengfenlei() {
        // 先查询一级节点   只有一级节点的父节点是0
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getParentid, 0);
        Category root = this.getOne(qw);
        // 转 v0
        CategoryVo fu = BeanHelper.copyProperties(root, CategoryVo.class);
        // 查询二级节点   根据一级节点查询
        LambdaQueryWrapper<Category> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Category::getParentid,root.getId());
        List<Category> erji = this.list(qw1);
        // 转VO
        List<CategoryVo> listerji = BeanHelper.copyWithCollection(erji, CategoryVo.class);
        // 在一级节点里面在设置二级节点
        fu.setCategoryTreeNodes(listerji);
        // 遍历二级节点
        for (CategoryVo categoryVo : listerji) {
            // 构建条件
            LambdaQueryWrapper<Category> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(Category::getParentid,categoryVo.getId());
            List<Category> sanjijiedian = this.list(qw2);
            // 转vo
            List<CategoryVo> sanji = BeanHelper.copyWithCollection(sanjijiedian, CategoryVo.class);
            //二级节点添加三级节点
            categoryVo.setCategoryTreeNodes(sanji);
        }
        return ResponseResult.okResult(fu);

    }
}
